<?php

namespace App\Http\Controllers\Merchants;
use App\calss\diLi\diLi;
use Illuminate\Http\Request;
use Validator;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\merchant;
class NewseditController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //修改商家用户信息
        return view('merchant.newsedit');die;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {       $key = '1d6577a66716940f16fabe795e5c8dd0';
            if(($request->address)){
            $address = $request->address;
        }else{
            $address = '北京市';
        }
            $a = file_get_contents('http://restapi.amap.com/v3/geocode/geo?key='."{$key}".'&address='."{$address}");
          $a= json_decode($a);
         
      
        //修改商家信息
          $validator = Validator::make($request->all(), [
            'name' => 'required',
            'address' => 'required',
               'intro' => 'required',
                  'notice' => 'required',
                     'send' => 'required',
                        'order' => 'required',
         
        ]);
          $id=$request->id;
          $arr = $request->all();
        
        $arr['cdata']=($a->geocodes['0']->location);
          $arr = array_except($arr, ['_token']);
          $arr = array_except($arr, ['id']);
        
          $new= DB::table('merchant')
            ->where('id', $id)
            ->update($arr);
            return back();
   
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
         //显示商家信息
        $mc = new Merchant;
        $data = $mc->select($id);


        return view('merchant.newsedit',['data'=>$data]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    //图片上传
    public function photo(Request $request)
    {

       
            $file = $request->file('photo');
           
            $id=$request->id;

             //确认上传的文件是否成功
            if($file->isValid()){

                //$picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $photo_path = "images/photo/";
                $filename = time().rand(1000,9999).".".$ext;
                $file->move($photo_path,$filename);
                $url = $photo_path.$filename;
                

                 $new= DB::table('merchant')
            ->where('id', $id)
            ->update(['photo'=>$url]);
            $homeInfo = session('homeInfo');
            $homeInfo->photo =$url;
         
            session(['homeInfo',$homeInfo]);
            return back();

            }else{

               return back();

            }
        

    }
     //资质上传
        public function aptitude(Request $request)
    {  

       

               $file =$request->file('aptitude');
           $id=$request->id;

             //确认上传的文件是否成功

            if($file->isValid()){
                $picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $photo_path = "images/photo/";
                $filename = time().rand(1000,9999).".".$ext;
                $file->move($photo_path,$filename);
                $url = $photo_path.$filename;

         

                 $new= DB::table('merchant')
                ->where('id', $id)
                ->update(['aptitude'=>$url]);
                
          $homeInfo = session('homeInfo');
            $homeInfo->aptitude =$url;
         
            session(['homeInfo',$homeInfo]);
                return back();
            }else{
               return back();

            }
   }


//图片预览
     public function look(Request $request)
    {
            $file = $request->file('photo');
 
//             // //确认上传的文件是否成功
            if($file->isValid()){
                $picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $photo_path = "images/aptitude/";
                $filename = time().rand(1000,9999).".".$ext;
                $file->move($photo_path,$filename);
          
                 $url = '/'.$photo_path.$filename;
                echo ($url);
            }
   }

//图片预览
     public function look2(Request $request)
    {
            $file = $request->file('aptitude');
// ​
//             // //确认上传的文件是否成功

            if($file->isValid()){
                $picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $photo_path = "images/aptitude/";
                $filename = time().rand(1000,9999).".".$ext;
                $file->move($photo_path,$filename);

           

                 $url = '/'.$photo_path.$filename;
                echo ($url);
            }
   }

}

